/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.em.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.cspimp.em.domain.Exercise;
import me.zhengjie.modules.cspimp.em.domain.vo.ExerciseVo;
import me.zhengjie.modules.cspimp.em.service.ExerciseService;
import me.zhengjie.modules.cspimp.em.service.dto.ExerciseQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://docs.auauz.net
* @author zmm
* @date 2020-05-24
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "应急演练管理")
@RequestMapping("/api/exercise")
public class ExerciseController {

    private final ExerciseService exerciseService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('exercise:list')")
    public void download(HttpServletResponse response, ExerciseQueryCriteria criteria) throws IOException {
        exerciseService.download(exerciseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询应急演练")
    @ApiOperation("查询应急演练")
    @PreAuthorize("@el.check('exercise:list')")
    public ResponseEntity<Object> query(ExerciseQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(exerciseService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增应急演练")
    @ApiOperation("新增应急演练")
    @PreAuthorize("@el.check('exercise:add')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "exerciseObjective", value = "演练目的", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "exerciseProcess", value = "演练流程", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "exerciseTitle", value = "应急演练标题", dataType = "String", required = true, paramType = "body"),
            @ApiImplicitParam(name = "advanceHour", value = "通知提前小时", dataType = "String",  paramType = "body"),
            @ApiImplicitParam(name = "exerciseDate", value = "演练时间", dataType = "String", required = true, paramType = "body"),
            @ApiImplicitParam(name = "isNotive", value = "公告通知状态", dataType = "boolean", required = true, paramType = "body")
    })
    public ResponseEntity<Object> create(@Validated @RequestBody ExerciseVo resources){
        return new ResponseEntity<>(exerciseService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改应急演练")
    @ApiOperation("修改应急演练")
    @PreAuthorize("@el.check('exercise:edit')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", required = true, paramType = "body"),
            @ApiImplicitParam(name = "exerciseObjective", value = "演练目的", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "exerciseProcess", value = "演练流程", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "exerciseTitle", value = "应急演练标题", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "advanceHour", value = "通知提前小时", dataType = "String",  paramType = "body"),
            @ApiImplicitParam(name = "exerciseDate", value = "演练时间", dataType = "String",  paramType = "body"),
            @ApiImplicitParam(name = "isNotive", value = "公告通知状态", dataType = "boolean",  paramType = "body")
    })
    public ResponseEntity<Object> update(@Validated @RequestBody ExerciseVo resources){
        exerciseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除应急演练")
    @ApiOperation("删除应急演练")
    @PreAuthorize("@el.check('exercise:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        exerciseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}